﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PLAYXPERT.Interfaces;
using PLAYXPERT.Controls;
using PLAYXPERT;
using PLAYXPERT.Windows;
using PLAYXPERT.Widgets;

namespace SkypePXP
{
    public partial class CallForm : PXPForm
    {
        BackLib.SkypeWrapper skypeWrapper;
        MainForm mainForm;
        List<SKYPE4COMLib.User> Users;
        bool IsGroup;
        public int ID;
        bool collapsed = false;

        public event AddPeopleHandler AddPeople;
        public delegate void AddPeopleHandler(bool isGroup, int ID);

        public CallForm(BackLib.SkypeWrapper skypeWrapper, ImageList imageList, MainForm mainForm)
        {
            InitializeComponent();
            this.skypeWrapper = skypeWrapper;
            this.mainForm = mainForm;
            listViewCallMembers.SmallImageList = imageList;
        }

        public void UpdateCallHoldStatus()
        {
            switch (skypeWrapper.GetCallStatusFriendly(ID, IsGroup))
            {
                case BackLib.SkypeWrapper.CallStatusFriendly.OnHold:
                    if (skypeWrapper.CanResumeCall(ID, IsGroup))
                    {
                        buttonToggleHold.Text = "Resume";
                        buttonToggleHold.Enabled = true;
                    }
                    else
                    {
                        buttonToggleHold.Text = "On Hold";
                        buttonToggleHold.Enabled = false;
                    }
                    break;
                default:
                    buttonToggleHold.Enabled = true;
                    buttonToggleHold.Text = "Hold";
                    break;
            }
        }

        public void DisplayNewCall(DataGridViewRow row)
        {
            IsGroup = (bool)row.Cells[0].Value;
            ID = (int)row.Tag;
            UpdateCallHoldStatus();
            labelCallTimer.Text = ((MainForm.DurationTimer)row.Cells[3].Tag).GetDurationText;
            List<ListViewItem> items = new List<ListViewItem>();
            Users = (List<SKYPE4COMLib.User>)row.Cells[0].Tag;
            foreach (SKYPE4COMLib.User user in Users)
            {
                ListViewItem item = new ListViewItem(skypeWrapper.GetDisplayName(user), (int)skypeWrapper.GetOnlineStatusFriendly(user));
                item.Tag = user.Handle;
                items.Add(item);
            }
            listViewCallMembers.Items.Clear();
            listViewCallMembers.Items.AddRange(items.ToArray());
        }

        public void UpdateUser(SKYPE4COMLib.User user)
        {
            for (int i = 0; i < listViewCallMembers.Items.Count; i++)
            {
                ListViewItem item = listViewCallMembers.Items[0];
                if (item.Tag.ToString() == user.Handle)
                {
                    item = new ListViewItem(skypeWrapper.GetDisplayName(user), (int)skypeWrapper.GetOnlineStatusFriendly(user));
                    return;
                }
            }
        }

        public bool Collapsed
        {
            set
            {
                collapsed = value;
                if (value)
                {
                    labelCollapse.Text = "-Expand Active Call-";
                    groupBoxActiveCall.Visible = false;
                    labelNoCalls.Visible = false;
                    mainForm.splitContainerMain.SplitterDistance = 10;
                    mainForm.splitContainerMain.IsSplitterFixed = true;
                }
                else
                {
                    labelCollapse.Text = "-Collapse-";
                    if (mainForm.dataGridViewCalls.Rows.Count == 0)
                    {
                        groupBoxActiveCall.Visible = false;
                        labelNoCalls.Visible = true;
                        mainForm.splitContainerMain.SplitterDistance = 25;
                    }
                    else
                    {
                        groupBoxActiveCall.Visible = true;
                        labelNoCalls.Visible = false;
                        mainForm.splitContainerMain.SplitterDistance = 62;
                    }
                    mainForm.splitContainerMain.IsSplitterFixed = false;
                }
            }
            get
            {
                return collapsed;
            }
        }

        private void label3_Click(object sender, EventArgs e)
        {
            if (Collapsed)
            {
                TriggerExpand();
            }
            else
            {
                TriggerCollapse();
            }
        }

        public void TriggerCollapse()
        {
            Collapsed = true;
        }

        public void TriggerExpand()
        {
            Collapsed = false;
        }

        private void ToggleButton_MouseDown(object sender, MouseEventArgs e)
        {
            mainForm.ToggleMute(!skypeWrapper.MuteStatus);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            AddPeople(IsGroup, ID);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            skypeWrapper.EndCall(ID, IsGroup);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            skypeWrapper.ToggleCallHold(ID, IsGroup);
        }

        private void sendIMToolStripMenuItem_Click(object sender, EventArgs e)
        {
            mainForm.CreateNewChat(new string[] { (string)listViewCallMembers.SelectedItems[0].Tag });
        }

        private void listView1_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right && listViewCallMembers.SelectedItems.Count == 1)
            {
                contextMenuStrip1.Show(MousePosition);
            }
        }
    }
}
